define([], function() {
    var formTableDirective = angular.module('app.directive.goBack', []);

    formTableDirective.directive('goBack', ['$compile', '$templateCache', 'xUtil', function($compile, $templateCache,
            xUtil) {
        return {
            restrict: 'EA',
            scope: {
                replace: '<'
            },
            link: function(scope, element) {
                var url = xUtil.location.history();
                if (url) {
                    var html = $compile($templateCache.get('templates/go-back.html'))(scope);
                    if (scope.replace) {
                        $(html).replaceAll(element);
                    } else {
                        $(html).appendTo(element);
                    }

                    xUtil.location.clear();

                    scope.goback = function() {
                        xUtil.location.back(url);
                    }
                } else {
                    if (element.children().length == 0) {
                        element.remove();
                    }
                }
            }
        };
    }])
    .run(['$templateCache', function($templateCache) {
        $templateCache.put('templates/go-back.html',
                '<button class="btn btn-default" type="button" ng-click="goback()"><li class="fa fa-reply"></li> 返回</button>');
    }]);;

    return formTableDirective;
});
